package com.zut.controller.house;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.zut.entity.House;
import com.zut.service.HouseService;
import com.zut.service.impl.HouseServiceImpl;

/**
 * Servlet implementation class editHouseServlet
 */
@WebServlet("/editHouseServlet")
@MultipartConfig
public class editHouseServlet extends HttpServlet {
	HouseService hs = new HouseServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String str = request.getParameter("action");
		if (str.equals("editShow")) {
			int h_id = Integer.parseInt(request.getParameter("h_id"));
			House house = hs.queryById(h_id);
			System.out.println("editHouseServlet中的h_id："+h_id);
			request.setAttribute("house", house);
			request.getRequestDispatcher("/view/flats/edit.jsp").forward(request, response);
		}
		if (str.equals("edit")) {
			House house = new House();
			house.setH_id(Integer.parseInt(request.getParameter("h_id")));
			house.setH_area(request.getParameter("h_area"));
			house.setH_estate(request.getParameter("h_estate"));
			house.setH_unitNumber(request.getParameter("h_unitNumber"));
			house.setH_floor(Integer.parseInt(request.getParameter("h_floor")));
			house.setH_roomNo(request.getParameter("h_roomNo"));
			house.setH_acreage(request.getParameter("h_acreage"));
			house.setH_direction(request.getParameter("h_direction"));
			house.setH_fitment(request.getParameter("h_fitment"));
			house.setH_isDoubleAir(Integer.parseInt(request.getParameter("h_isDoubleAir")));
			house.setH_limit(Integer.parseInt(request.getParameter("h_limit")));
			house.setH_facility(request.getParameter("h_facility"));
			house.setH_price(Double.parseDouble(request.getParameter("h_price")));
			System.out.println(request.getParameter("h_status"));
			house.setH_status(Integer.parseInt(request.getParameter("h_status")));
			//house.setH_img(request.getParameter("h_img"));
			System.out.println("------------开始获取真实路径---存储路径------------------------------------");
			//获取真实路径---存储路径
	        String savePath = request.getServletContext().getRealPath("/image");
	        System.out.println("修改的===真实路径是"+savePath);
	        //获取上传的文件集合
	        List<Part> parts = (List<Part>) request.getParts();
	        System.out.println("parts.size()=="+parts.size());
	        
	        String header = parts.get(14).getHeader("content-disposition");
	        //获取文件名
	        String fileName = getFileName(header);
	        //把文件写到指定路径
	        parts.get(14).write(savePath+File.separator+fileName);
	        house.setH_img("/image/"+fileName);
			
			house.setH_address(request.getParameter("h_address"));
			
			hs.updateHouse(house);
			response.sendRedirect(request.getContextPath()+"/findHouseServlet?currentPage=1");
		}
		
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	 public String getFileName(String header) {
        String[] tempArr1 = header.split(";");
        String[] tempArr2 = tempArr1[2].split("=");
        //获取文件名，兼容各种浏览器的写法
        String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\")+1).replaceAll("\"", "");
        return fileName;
    }

}
